package com.aacar.op.web.order;

import com.aacar.op.entity.order.Order;
import com.aacar.op.entity.washer.Washer;
import com.aacar.op.service.order.OrderService;
import com.aacar.op.service.washer.WasherService;
import com.aacar.op.utils.Constant;
import com.aacar.op.utils.DateUtil;
import com.aacar.op.utils.enumeration.MenuPermissions;
import com.aacar.op.utils.pojo.ActionResult;
import com.aacar.op.utils.pojo.datatables.DataTablesResult;
import com.aacar.op.utils.web.PageUtils;
import com.aacar.op.utils.web.Servlets;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 订单管理
 * @author Chao
 *
 */
@Controller
@RequestMapping("/op/order")
public class OrderController {
	
    private static Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;
    
    @RequestMapping(method = {RequestMethod.GET})
    public String list() {
        return "op/order/order";
    }
    
    /**
     * datatables json
     */
    @RequestMapping(value = "datatables", method = {RequestMethod.GET, RequestMethod.POST})
    public
    @ResponseBody
    DataTablesResult<Washer> datatables(HttpServletRequest request,
                                      @RequestParam(value = "search_startDate", required = false)
                                      @DateTimeFormat(pattern = Constant.DATE_PATTERN_SHORT) Date startDate,
                                      @RequestParam(value = "search_endDate", required = false)
                                      @DateTimeFormat(pattern = Constant.DATE_PATTERN_SHORT) Date endDate) {

        Pageable pageable = PageUtils.pageableFromDateTables(request);
        //使用Map传参到Service层,便于扩展.
        //获取"search_"的参数传as Map.
        Map<String, Object> params = Servlets.getParametersStartingWith(request, "search_");
        startDate = DateUtil.safeStartDate(startDate);
        endDate = DateUtil.safeEndDate(endDate);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        PageInfo<Order> pageInfo = orderService.getPage(params, pageable);
        return DataTablesResult.build(pageInfo);
    }

    /**
     * 接单页面:: GET
     */
    @RequestMapping(value = "rece", method = RequestMethod.GET)
    public String input(@ModelAttribute("entity") Order entity, Model model) {
        model.addAttribute("menuPerSelect2JsonArr", MenuPermissions.asSelect2JsonArr());
        return "op/order/order-rece";
    }


    /**
     * update :: POST
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public
    @ResponseBody
    ActionResult input(@Valid @ModelAttribute("entity") Order entity, BindingResult result) {

        if (result.hasErrors()) {
            logger.warn(result.toString());
            return ActionResult.failure("数据不合法,保存失败。");
        }
        
        ActionResult actionResult;
        try {
            int res = orderService.saveOrUpdate(entity);
            if(res == 100||res == 200||res == 300||res == 400||res == 500){
                actionResult = ActionResult.failure("订单&quot;" + entity.getOrderNo() + "&quot;操作失败，订单状态不符，请刷新页面！");
            }else{
                actionResult = ActionResult.success("订单&quot;" + entity.getOrderNo() + "&quot;操做成功。");
            }
        } catch (Exception e) {
            logger.error("用户保存异常:{}", e);
            actionResult = ActionResult.failure("订单&quot;" + entity.getOrderNo() + "&quot;操作失败。");
        }
        return actionResult;
    }
    

    
}
